<template>
  <div class="article-detail">
    <h2 class="text-lg font-semibold">{{ articleDetail.title }}</h2>
    <div class="author-info flex items-center mb-4">
      <img :src="avatar" alt="" />
      <span class="ml-2 font-semibold text-base">{{ articleDetail.author?.nickName }}</span>
      <span class="ml-1 mr-1">-</span>
      <span v-if="articleDetail.author?.description" class="text-sm flex-1 truncate">{{
        articleDetail.author?.description
      }}</span>
    </div>
    <img
      v-if="articleDetail.image?.url"
      class="max-w-full my-0 m-auto"
      :src="articleDetail.image?.url"
      alt=""
    />
    <div v-html="articleDetail.content" class="mt-4"></div>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, onMounted, reactive, watch, ref } from 'vue'
import { getArticleDetail, ArticleDto } from '@/services/ApiServer/ArticleRequest'
import { useRoute } from 'vue-router'
import { assign, forIn } from 'lodash'

export default defineComponent({
  name: 'columnDetail',
  setup() {
    const route = useRoute()
    const articleId = ref(route.params.articleId as string)
    const avatar = computed(() => {
      return (
        articleDetail.image?.url ||
        new URL('../../../assets/images/avatar.svg', import.meta.url).href
      )
    })
    let articleDetail: Partial<ArticleDto> = reactive({})
    const getArticle = async () => {
      const res = await getArticleDetail(articleId.value)
      if (res && res.code === 0 && res.data) {
        assign(articleDetail, res.data)
      }
    }
    watch(route, (val) => {
      if (val.path.indexOf('/column/detail/') > -1) {
        articleId.value = val.params.articleId as string
        forIn(articleDetail, (value) => {
          value = ''
        })
        getArticle()
      }
    })
    onMounted(() => {
      getArticle()
    })
    return {
      articleDetail,
      avatar,
    }
  },
})
</script>

<style lang="scss" scoped>
.article-detail {
  width: 700px;
  margin: 10px auto;
  background: #fff;
  border-radius: 2px;
  box-shadow: 0 1px 3px hsl(0deg 0% 7% / 10%);
  padding: 16px 20px;
  min-height: calc(100vh - 240px);
  .author-info {
    margin-top: 6px;
    img {
      width: 24px;
      height: 24px;
    }
  }
}
</style>
